package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private String username=null;
    public String password=null;
    Button regButton=null;
    Button logButton=null;
    EditText unameEdit,pwdEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username=unameEdit.getText().toString();
                password=pwdEdit.getText().toString();
                if(UserInfoIO.storeUser(username,password,MainActivity.this))
                {
                    Toast.makeText(MainActivity.this,username+":"+password+"  注册成功!",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this,"注册失败!",Toast.LENGTH_LONG).show();
                }
            }
        });
        logButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username=unameEdit.getText().toString();
                password=pwdEdit.getText().toString();
                Map<String,String> userMap=UserInfoIO.loadUser(MainActivity.this);
                if(userMap.get(username)!=null)
                {
                    if(userMap.get(username).equals(password))
                    {
                        Toast.makeText(MainActivity.this,username+":"+password+"  登录成功!",Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    Toast.makeText(MainActivity.this, "用户名不存在!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void init()
    {
        regButton=findViewById(R.id.register);
        logButton=findViewById(R.id.login);
        unameEdit=findViewById(R.id.username);
        pwdEdit=findViewById(R.id.password);
    }
}